export default class ButtonPermissionUtils {
	/**
	 * 获取当前用户的 ID
	 * @returns {string|null} 用户 ID，如果未登录则返回 null
	 */
	static getUserId() {
		const userInfo = JSON.parse(sessionStorage.getItem('userInfo'));
		return userInfo?.id || null;
	}

	/**
	 * 判断是否有权限进行风险辨识
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canRiskSafe(row) {
		return row.state * 1 === 0;
	}

	/**
	 * 判断是否有权限进行措施确认
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canCompile(row) {
		const ids = [row.bp_cs_true_people_id, row.bz_people_id];
		const combinedString = ids.join(',');
		const userId = this.getUserId();
		return row.state * 1 === 1 && this.isInList(userId, combinedString);
	}

	/**
	 * 判断是否有权限进行气体分析
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canGas(row) {
		const userId = this.getUserId();
		return [2, 3, 4].includes(row.state) && this.isInList(userId, row.gas_people_id);
	}

  	/**
	 * 判断是否有权限进行交底（安全交底人）
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canTell(row) {
		const userId = this.getUserId();
		return row.state * 1 === 3 && this.isInList(userId, row.work_tell_people_id);
	}

	/**
	 * 判断是否有权限作业负责人接受交底（作业负责人）
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canTask(row) {
		const userId = this.getUserId();
		return row.state * 1 === 3 && this.isInList(userId, row.work_take_people)
	}

  	/**
	 * 判断是否有权限施工监护人接受交底（施工监护人）
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canDute(row) {
		const userId = this.getUserId();
		return row.state * 1 === 3 && this.isInList(userId, row.work_dute_people)
	}

	/**
	 * 判断是否有权限属地监护人接受交底（属地监护人）
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canDuteP(row) {
		const userId = this.getUserId();
		return row.state * 1 === 3 && this.isInList(userId, row.work_dute_people_p)
	}

  
	/**
	 * 判断是否有权限进行审核 审核人选择
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canAudit(row) {
		const userId = this.getUserId();
		return row.state * 1 === 5 && this.isInList(userId, row.work_dute_people_p)
	}

  
	/**
	 * 判断是否有权限 开始作业
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canStart(row) {
		const userId = this.getUserId();
		const { ticket_people_id, work_dute_people_id, work_dute_people_p_id, zp_dute_id } = row;
		return (
			this.isInList(userId, ticket_people_id) ||
			this.isInList(userId, work_dute_people_id) ||
			this.isInList(userId, work_dute_people_p_id) ||
			this.isInList(userId, zp_dute_id)
		);
	}

	/** 
	 * 判断是否有权限 暂停作业
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canStop(row) {
		const userId = this.getUserId();
		const { ticket_people_id, work_dute_people_id, work_dute_people_p_id, zp_dute_id } = row;
		return (
			this.isInList(userId, ticket_people_id) ||
			this.isInList(userId, work_dute_people_id) ||
			this.isInList(userId, work_dute_people_p_id) ||
			this.isInList(userId, zp_dute_id)
		);
	}

  
	/**
	 * 判断是否有权限结束作业
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canFinishPermission(row) {
		const userId = this.getUserId();
		const { ticket_people_id, work_dute_people_id, work_dute_people_p_id, zp_dute_id } = row;
		return (
			this.isInList(userId, ticket_people_id) ||
			this.isInList(userId, work_dute_people_id) ||
			this.isInList(userId, work_dute_people_p_id) ||
			this.isInList(userId, zp_dute_id)
		);
	}

  	/**
	 * 判断是否可以结束作业
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canFinish(row) {
		return row.state === '2' && row.start_work_flag === '1' && !row.is_accep && !row.is_accep_p;
	}






	/**
	 * 判断是否有删除权限
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canDelete(row) {
		const userId = this.getUserId();
		return row.createpeopleid === userId && [0, 1, 3].includes(row.state);
	}




	/**
	 * 判断是否有权限进行作业单位验收
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canAccept(row) {
		const userId = this.getUserId();
		return this.isInList(userId, row.work_accep_id);
	}

	/**
	 * 判断是否有权限进行属地单位验收
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canAcceptP(row) {
		const userId = this.getUserId();
		return this.isInList(userId, row.work_accep_p_id);
	}

	/**
	 * 判断是否有权限进行修改确认
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canUpdate(row) {
		const userId = this.getUserId();
		return this.isInList(userId, row.ticket_people_id);
	}

	/**
	 * 判断是否有权限提交监护人意见
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canOpinion(row) {
		const userId = this.getUserId();
		return this.isInList(userId, row.work_dute_people_p_id);
	}

	/**
	 * 判断是否有权限提交被指派监护人意见
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canOpinionZp(row) {
		const userId = this.getUserId();
		return this.isInList(userId, row.zp_dute_id);
	}

	/**
	 * 判断是否有权限更换属地监护人
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canChangeDute(row) {
		const userId = this.getUserId();
		const { createpeopleid, work_dute_people_p_id, zp_dute_id } = row;
		return this.isInList(userId, createpeopleid) || this.isInList(userId, work_dute_people_p_id) || this.isInList(userId, zp_dute_id);
	}

	/**
	 * 判断是否有权限更换气体分析人
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canChangeGas(row) {
		const userId = this.getUserId();
		const { createpeopleid, work_dute_people_p_id, zp_dute_id, work_accep_p_id } = row;
		return (
			this.isInList(userId, createpeopleid) ||
			this.isInList(userId, work_dute_people_p_id) ||
			this.isInList(userId, zp_dute_id) ||
			this.isInList(userId, work_accep_p_id)
		);
	}





	/**
	 * 判断是否有权限接受交底（动火人）
	 * @param {Object} row 当前行数据
	 * @returns {boolean}
	 */
	static canAccess(row) {
		const userId = this.getUserId();
		return this.isInList(userId, row.work_people_id);
	}



	/**
	 * 判断用户是否在列表中
	 * @param {string} userId 当前用户ID
	 * @param {string} list 用户列表（逗号分隔）
	 * @returns {boolean}
	 */
	static isInList(userId, list) {
		// console.log(list.split(','))
		if (!list) return false;
		return list.split(',').includes(userId);
	}
}
